define(function (require) {
    require('common/fileuploader');
    var Base = require('group/common/main');
    var utils = require('common/utils/home');
    require('validate');
    var Mask = require('common/mask');
    var mask = new Mask();
    var Cropper = require('common/cropper');

    //idname 倒计时id名称， count倒计时秒数，url跳转地址
    function jump(count, url) {
        window.setTimeout(function () {
            count--;
            if (count > 0) {
                $('#jumpTo').html(count);
                jump(count, url);
            } else {
                location.href = url;
            }
        }, 1000);
    }

    return Base.extend({
        initialize: function (params) {
            var _this = this;
            Base.prototype.initialize.apply(this, [params]);
            if (_this['page_' + _global.a]) {
                _this['page_' + _global.a]();
            }
        },
        page_login: function () {
            this.page_register();
        },
        page_forget_password: function () {
            var _this = this;
            $('.JCaptcha,.JRefreshCaptcha').click(function () {
                $('.JCaptcha').attr('src', _.U({
                    m: 'public',
                    a: 'captcha',
                    t: Math.random()
                }));
            });
            $('.J_check_tele').blur(function () {
                var _this = $(this);
                var tele = _this.val();
                if (tele) {
                    utils.ajaxPost(_.extend({tele: tele}, {m: 'passport', a: 'check_tele'}), function (resp) {
                        $('.error_tele').remove();
                        $('.J_sms').show();
                    }, function (resp) {
                        if ($('.error_tele').size() == 0) {
                            _this.after('<label class="has-error help-block error_tele" style="display: block;">手机号没有注册过</label>');
                            $('.J_sms').hide();
                        }
                    });
                }
            });

            $('.J_form').validate({});
            if ("undefined" == typeof forget_password_step) {
                forget_password_step = 1;
            }
            if (forget_password_step == 4) {
                var count = 5;
                var url = _.U({
                    m: 'user',
                    a: 'index'
                });
                jump(count, url);
            }
        },
        page_register: function () {
            var _this = this;//J_sms
            $('.J_send_code').click(function () {
                var $this = $(this);
                var tele = _.trim($('form input[name="tele"]').val());
                if (_.empty(tele)) return;
                utils.ajaxPost({
                    a: 'getSMSCode',
                    tele: tele
                }, function (resp) {
                    var seconds = 60;
                    $this.addClass('sms_code');
                    setInterval(function () {
                        $this.text(seconds + '秒后重发');
                        if (seconds > 0) {
                            seconds--;
                        } else {
                            $this.html('获取验证码');
                            $this.removeClass('sms_code');
                            return;
                        }
                    }, 1000);
                });
            });

            $('.J_check_tele').blur(function () {
                var _this = $(this);
                var tele = _this.val();
                if (tele) {
                    utils.ajaxPost(_.extend({tele: tele}, {m: 'passport', a: 'check_tele'}), function (resp) {
                        if ($('.error_tele').size() == 0) {
                            _this.after('<label class="has-error help-block error_tele" style="display: block;">手机号已注册</label>');
                        }
                        $('#J_send_code').hide();
                    }, function (resp) {
                        $('.error_tele').remove();
					    $('#J_send_code').show();
                    });
                }
            });

            $('.J_form').validate({
                submitHandler: function (form) {
                    var params = $('.J_form').serializeJson();
                    utils.ajaxPost(_.extend(params, {g: 'team', m: 'passport', a: _global.a}), function (resp) {
                        mask.success(resp.msg);
                        if (resp.url) {
                            _this.redirect(resp.url, 1);
                        } else {
                            _this.redirect(_.U({m: 'user', a: 'index'}), 1);
                        }
                    }, function (resp) {
                        mask.error(resp.msg);
                    });
                }
            });
        }
    });
});